﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net.NetworkInformation;

using System.Net.Sockets;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace DynDns
{
    public partial class grid2 : System.Web.UI.Page
    {        
        ChangeConfigValue ccV;                  // Class to Read/Write IP
        String currentIP;
        int timeout = 500;                      // 500ms

        protected void Page_Load(object sender, EventArgs e)
        {
            SiteMapDataSource1.Provider = SiteMap.Providers["Default"];
            ccV = new ChangeConfigValue();
            currentIP = ccV.readIP("whs.fly-air.de");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // http://stackoverflow.com/questions/4199482/databind-a-gridview-populated-in-the-code-behind
                // http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26842831.html

                SiteMapNode myR = (SiteMapNode)e.Row.DataItem;

                int idR = e.Row.RowIndex;


                string fggf = myR.Description;
                
                //e.Row. = "ffgd";

                //e.Row.DataItem = "";

                //for( int i=0; i< e.Row.Cells.Count; i++)
                //{
                //    if(e.Row.Cells[i].i                
                
                //}

            }

        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow) 
            {
                // Check IF allowed for User, read Discription
                SiteMapNode myR = (SiteMapNode)e.Row.DataItem;
                if (myR.Description.ToString() == "")
                {
                    e.Row.Visible = true;               // sichtbar für alle

                    // Cell 1 HyperLink
                    //   e.Row.Cells[1].Text = "IP" + e.Row.Cells[1].Text;
                    
                    HyperLink hlinke = (HyperLink)e.Row.Cells[1].FindControl("HyperLinkServerUrl");
                    string nav = hlinke.NavigateUrl;
                    hlinke.NavigateUrl = "http://" + currentIP + nav;
                    hlinke.Text = hlinke.NavigateUrl;                    

                    Panel pOn = (Panel)e.Row.Cells[2].FindControl("PanelOnline");
                    Panel pOff = (Panel)e.Row.Cells[2].FindControl("PanelOffLine");
                    if ( ConnectionAvailable(hlinke.NavigateUrl) == true )
                    {
                        pOn.Visible = true;
                        pOff.Visible = false;
                        e.Row.Cells[2].ToolTip = "Online";
                    }
                    else
                    {
                        pOn.Visible = false;
                        pOff.Visible = true;
                        e.Row.Cells[2].ToolTip = "Offline";
                    }
                }
                else {
                    e.Row.Visible = false;               // nicht sichtbar
                }
            
            }



        }

        // TEST Connection
        public bool ConnectionAvailable(string strServer)
        {
            ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) {
                return true;
            };

            try
            {
                HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create(strServer);
                reqFP.Timeout = timeout;

                HttpWebResponse rspFP = (HttpWebResponse)reqFP.GetResponse();
                if (HttpStatusCode.OK == rspFP.StatusCode)
                {
                    // HTTP = 200 - Internet connection available, server online
                    rspFP.Close();
                    return true;
                }
                else
                {
                    // Other status - Server or connection not available
                    rspFP.Close();
                    return false;
                }
            }
            catch (WebException we)
            {
                // Exception - connection not available
                if (we.Response != null | we.Status.ToString() == "TrustFailure")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }





    }
}